package cn.com.headfree.datapermits.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;

import cn.com.headfree.datapermits.dao.IDataPermitsDao;
import cn.com.headfree.entity.TDataRolePermission;
import cn.com.headfree.entity.TMouldInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.util.DateUtils;

/**  
 * @Title  DataPermitsDaoImpl.java   
 * @Package cn.com.headfree.datapermits.dao.impl   
 * @Description 数据权限信息daoImpl
 * @author yangwang 
 * @date  2020年7月3日 下午3:45:54   
 */
@Repository("dataPermitsLogDao")
public class DataPermitsDaoImpl extends BaseDaoImpl<TDataRolePermission> implements IDataPermitsDao {

	/**
	 * 插入记录
	 * @param permits
	 *  */
	@Override
	public void insert(TDataRolePermission permits) {
		String sql = "insert into t_data_role_permission(id,role_code,role_name,business_type_id,add_flag,del_flag,modify_flag,query_flag,creater_id,creater_name,create_time,updater_id,updater_name,update_time,del_sign,is_valid) values";
    	StringBuilder sb = new StringBuilder(sql);
		sb.append("(#{id,jdbcType=VARCHAR},")
		.append("#{role_code,jdbcType=VARCHAR},")
		.append("#{role_name,jdbcType=VARCHAR},")
		.append("#{business_type_id,jdbcType=VARCHAR},")
		.append("#{add_flag,jdbcType=VARCHAR},")
		.append("#{del_flag,jdbcType=VARCHAR},")
		.append("#{modify_flag,jdbcType=VARCHAR},")
		.append("#{query_flag,jdbcType=VARCHAR},")
		.append("#{creater_id,jdbcType=VARCHAR},")
		.append("#{creater_name,jdbcType=VARCHAR},")
		.append("#{create_time},")
		.append("#{updater_id,jdbcType=VARCHAR},")
		.append("#{updater_name,jdbcType=VARCHAR},")
		.append("#{update_time},")
		.append("#{del_sign,jdbcType=INTEGER},")
		.append("#{is_valid,jdbcType=INTEGER})");
		
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", permits.getId());
        params.put("role_code", permits.getRoleCode());
        params.put("role_name", permits.getRoleName());
        params.put("business_type_id", permits.getBusinessTypeId());
        params.put("add_flag", permits.getAddFlag());
        params.put("del_flag", permits.getDelFlag());
        params.put("modify_flag", permits.getModifyFlag());
        params.put("query_flag", permits.getQueryFlag());
        params.put("creater_id", permits.getCreaterId());
        params.put("creater_name", permits.getCreaterName());
        params.put("create_time", permits.getCreateTime());
        params.put("updater_id", permits.getUpdaterId());
        params.put("updater_name", permits.getUpdaterName());
        params.put("update_time", permits.getUpdateTime());
        params.put("del_sign", 0);
        params.put("is_valid", permits.getIsValid());
        
        this.executeSql(sb.toString(), params);
	}
	
	/**
	 * 编辑记录
	 * @param permits
	 *  */
	@Override
	public void edit(TDataRolePermission permits) {
		String sql = "update t_data_role_permission set ";
    	StringBuilder sb = new StringBuilder(sql);
		sb.append("role_code=#{role_code,jdbcType=VARCHAR},")
		.append("role_name=#{role_name,jdbcType=VARCHAR},")
		.append("business_type_id=#{business_type_id,jdbcType=VARCHAR},")
		.append("add_flag=#{add_flag,jdbcType=VARCHAR},")
		.append("del_flag=#{del_flag,jdbcType=VARCHAR},")
		.append("modify_flag=#{modify_flag,jdbcType=VARCHAR},")
		.append("query_flag=#{query_flag,jdbcType=VARCHAR},")
		.append("updater_id=#{updater_id,jdbcType=VARCHAR},")
		.append("updater_name=#{updater_name,jdbcType=VARCHAR},")
		.append("update_time=#{update_time},")
		.append("is_valid=#{is_valid,jdbcType=INTEGER}")
		.append(" where id=#{id,jdbcType=VARCHAR}");
		
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", permits.getId());
        params.put("role_code", permits.getRoleCode());
        params.put("role_name", permits.getRoleName());
        params.put("business_type_id", permits.getBusinessTypeId());
        params.put("add_flag", permits.getAddFlag());
        params.put("del_flag", permits.getDelFlag());
        params.put("modify_flag", permits.getModifyFlag());
        params.put("query_flag", permits.getQueryFlag());
        params.put("updater_id", permits.getUpdaterId());
        params.put("updater_name", permits.getUpdaterName());
        params.put("update_time", permits.getUpdateTime());
        params.put("is_valid", permits.getIsValid());
        
        this.executeSql(sb.toString(), params);
	}
	
	/**
	 * 删除记录
	 * @param user
	 * @param ids
	 *  */
	@Override
	public void delete(User user,String ids) {
		if(StringUtils.isNotBlank(ids)) {
			String sql = "update t_data_role_permission set";
			StringBuilder sb=new StringBuilder(sql);
			Map<String,Object> params = new HashMap<String,Object>();
			sb.append(" del_sign=1,")
			.append(" update_time=#{update_time},");
			params.put("update_time", new Date());
			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
				sb.append(" updater_id=#{updater_id},")
				.append(" updater_name=#{updater_name}");
				params.put("updater_id", user.getFax());
				params.put("updater_name", user.getName());
				
			}
			sb.append(" where");
			String[] idArr = ids.split(",");
			if((null != idArr)&&(0 < idArr.length)) {
				Boolean bool = false;
				for(int i=0;i<idArr.length;i++) {
					if(StringUtils.isNotBlank(idArr[i])) {
						if(bool) {
							sb.append(" or id=#{id"+i+"}");
						}else {
							sb.append(" id=#{id"+i+"}");
						}
						bool = true;
						params.put("id"+i, idArr[i]);
					}
				}
				if(bool) {
					this.executeSql(sb.toString(), params);
				}
			}
		}
	}
	
	/**
	 * 获取列表记录
	 * @param permits
	 *  */
	@Override
	public List<TDataRolePermission> getList(TDataRolePermission permits){
		String sql = "select distinct id,role_code,role_name,business_type_id,add_flag,del_flag,modify_flag,query_flag,creater_id,creater_name,create_time,updater_id,updater_name,update_time,is_valid";
    	StringBuilder sb = new StringBuilder(sql);
    	sb.append(" from t_data_role_permission")
    	.append(" where del_sign=0");
    	
    	List<Object> paramList = new ArrayList<Object>();
    	if(null != permits) {
    		if(StringUtils.isNotBlank(permits.getRoleCode())) {
    			StringBuilder roleSb = new StringBuilder();
    			String[] roleIds = permits.getRoleCode().split(",|;|，|；");
    			if((null != roleIds)&&(0 < roleIds.length)) {
    				roleSb.append(" and (");
    				Boolean flag = false;
    				for(int i=0;i<roleIds.length;i++) {
    					if(StringUtils.isNotBlank(roleIds[i])) {
    						if(0 < i) {
    							roleSb.append(" or ");
        					}
    						roleSb.append(" role_code=?");
        					paramList.add(permits.getRoleCode());
        					flag = true;
    					}
    				}
    				roleSb.append(")");
    				if(flag) {
    					sb.append(roleSb.toString());
    				}
    			}
    		}
    		if(StringUtils.isNotBlank(permits.getBusinessTypeId())) {
    			sb.append(" and business_type_id=?");
    			paramList.add(permits.getBusinessTypeId());
    		}
			if(StringUtils.isNotBlank(permits.getAddFlag())) {
				sb.append(" and add_flag=?");
				paramList.add(permits.getAddFlag());
			}
			if(StringUtils.isNotBlank(permits.getDelFlag())) {
				sb.append(" and del_flag=?");
				paramList.add(permits.getDelFlag());
			}
			if(StringUtils.isNotBlank(permits.getModifyFlag())) {
				sb.append(" and modify_flag=?");
				paramList.add(permits.getModifyFlag());
			}
			if(StringUtils.isNotBlank(permits.getQueryFlag())) {
				sb.append(" and query_flag=?");
				paramList.add(permits.getQueryFlag());
			}
			if(null != permits.getStartTime()) {
				sb.append(" and upf.CREATE_TIME >= ?");
    			paramList.add(new Timestamp(DateUtils.getStartTimeOfDay(permits.getStartTime()).getTime()));
			}
			if(null != permits.getEndTime()) {
				sb.append(" and upf.CREATE_TIME <= ?");
    			paramList.add(new Timestamp(DateUtils.getEndTimeOfDay(permits.getEndTime()).getTime()));
			}
    	}
    	sb.append(" order by update_time desc");
				
		return this.findAllBySQL(TDataRolePermission.class, sb.toString(), null, false, paramList.toArray());
	}
	
	/**
	 * 获取分页记录
	 * @param permits
	 *  */
	@Override
	public void getPage(Page<TDataRolePermission> page,TDataRolePermission permits) {
		String sql = "select distinct id,role_code,role_name,business_type_id,add_flag,del_flag,modify_flag,query_flag,creater_id,creater_name,create_time,updater_id,updater_name,update_time,is_valid";
    	StringBuilder sb = new StringBuilder(sql);
    	sb.append(" from t_data_role_permission")
    	.append(" where del_sign=0");
    	
    	List<Object> paramList = new ArrayList<Object>();
    	if(null != permits) {
    		if(StringUtils.isNotBlank(permits.getRoleCode())) {
    			StringBuilder roleSb = new StringBuilder();
    			String[] roleIds = permits.getRoleCode().split(",|;|，|；");
    			if((null != roleIds)&&(0 < roleIds.length)) {
    				roleSb.append(" and (");
    				Boolean flag = false;
    				for(int i=0;i<roleIds.length;i++) {
    					if(StringUtils.isNotBlank(roleIds[i])) {
    						if(0 < i) {
    							roleSb.append(" or ");
        					}
    						roleSb.append(" role_code=?");
        					paramList.add(permits.getRoleCode());
        					flag = true;
    					}
    				}
    				roleSb.append(")");
    				if(flag) {
    					sb.append(roleSb.toString());
    				}
    			}
    		}
    		if(StringUtils.isNotBlank(permits.getBusinessTypeId())) {
    			sb.append(" and business_type_id=?");
    			paramList.add(permits.getBusinessTypeId());
    		}
			if(StringUtils.isNotBlank(permits.getAddFlag())) {
				sb.append(" and add_flag=?");
				paramList.add(permits.getAddFlag());
			}
			if(StringUtils.isNotBlank(permits.getDelFlag())) {
				sb.append(" and del_flag=?");
				paramList.add(permits.getDelFlag());
			}
			if(StringUtils.isNotBlank(permits.getModifyFlag())) {
				sb.append(" and modify_flag=?");
				paramList.add(permits.getModifyFlag());
			}
			if(StringUtils.isNotBlank(permits.getQueryFlag())) {
				sb.append(" and query_flag=?");
				paramList.add(permits.getQueryFlag());
			}
			if(null != permits.getStartTime()) {
				sb.append(" and upf.CREATE_TIME >= ?");
    			paramList.add(new Timestamp(DateUtils.getStartTimeOfDay(permits.getStartTime()).getTime()));
			}
			if(null != permits.getEndTime()) {
				sb.append(" and upf.CREATE_TIME <= ?");
    			paramList.add(new Timestamp(DateUtils.getEndTimeOfDay(permits.getEndTime()).getTime()));
			}
    	}
    	sb.append(" order by update_time desc");
				
    	this.findPageBySql(TDataRolePermission.class, sb.toString(), page, paramList.toArray());
	}
	
	/**
	 * 获取模板列表
	 * @param info
	 * @return List<TMouldInfo>
	 *  */
	@Override
	public List<TMouldInfo> getMouldInfos(TMouldInfo info){
		String sql = "select distinct ID,MOULD_NAME,TYPE,VALUE";
    	StringBuilder sb = new StringBuilder(sql);
    	sb.append(" from t_mould_info")
    	.append(" where del_sign=0");
    	
    	List<Object> paramList = new ArrayList<Object>();
    	if(null != info) {
    		if(StringUtils.isNotBlank(info.getId())) {
    			sb.append(" and id=?");
    			paramList.add(info.getId());
    		}
    		if(StringUtils.isNotBlank(info.getMouldName())) {
    			sb.append(" AND MOULD_NAME LIKE concat('%',?,'%') ESCAPE '/'");
                String replaceStr = info.getMouldName().replaceAll("%", "/%").replaceAll("_", "/_");
                paramList.add(replaceStr);
    		}
    	}
				
		return this.findAllBySQL(TMouldInfo.class, sb.toString(), null, false, paramList.toArray());
	}
	
}
